home *** CD-ROM | disk | FTP | other *** search
- Path: Rezonet.net!news
- From: ray@ultimate-tech.com (Ray Dunn)
- Newsgroups: comp.lang.c
- Subject: Re: Why isn't return address in the stack protected?
- Date: 24 Jan 1996 17:32:46 GMT
- Organization: Ultimate Technographics Inc.
- Message-ID: <4e5qfv$1587@ns.RezoNet.NET>
- References: <DLo4xF.MH@un.seqeb.gov.au> <1996Jan24.032914.8805@ohstpy>
- NNTP-Posting-Host: 204.19.230.7
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In article <DLo4xF.MH@un.seqeb.gov.au>, al012@svtstu.seqeb.gov.au
- (Anthony Lee) writes:
- > void log_event(void)
- > {
- > char message[256];
- > int i;
- >
- > sprintf(message,
- > " Event, node: %d, seq:%08.8x, type:%02.2d,
- > length:%03.3d, data: ",
- > nodeid, me.seqno, me.type, me.length);
- >
- > for ( i = 0; i < me.length; i++)
- > sprintf(&message[strlen(message)],"%02.2x
- > ",me.data.array[i] & 0xff);
- > printf("%s\n", message);
- >
- > }
- >
- > In the above code if the me.length is such that the resultant
- > string is longer than the size of message (> 256 bytes). Then
- > my C program would happily sprintf into message. The problem is
- > that it starts writing all over the stack causing the program
- > to fall over. My question is why can't the run time environment
- > protect the stack from such access and flag an error?
-
- Automatic variables are usually allocated from the same area of memory
- as the stack (it's difficult to not use system-specific terms here).
- There is no way that a normal 'C' environment can differentiate your
- writes to message and writes to outside those 256 bytes. In order to
- do so, the compiler would have to plant complex bounds checking code,
- and in this case too, the "sprintf" and all the library routines would
- also need to know the size of their arguments and check that you
- remained within bounds. Slow cumbersome run-time!
-
- If message was not an automatic, but a global or static, then on some
- systems it would occupy its own "data segment" (there I said it was
- difficult to avoid system specific terms!), and the hardware would
- cause an exception if an attempt was made to write outside its legal
- bounds.
-
- --
- Ray Dunn (opinions are my own) | Phone: (514) 938 9050
- Montreal | Phax : (514) 938 5225
- ray@ultimate-tech.com | Home : (514) 630 3749
-
-